package com.mobilereports.view;

import java.util.Comparator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.mobilereports.R;
import com.mobilereports.model.ReportColumn;
import com.mobilereports.model.Sample;
import com.mobilereports.sort.ComparatorManager;

public class ChartActivity extends Activity
{
	private Button backToColumnSelectionButton;
	private Button drillUpButton;
	private ChartView chartView;
	private ArrayAdapter<Comparator<Sample>> sortOrderArrayAdapter;
	
	private Spinner measureSpinner;
	private Spinner sortSpinner;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chart);
		
		chartView = (ChartView) findViewById(R.id.chart);
		chartView.setVisibility(View.VISIBLE);		

		measureSpinner = (Spinner) findViewById(R.id.measureSpinner);
		
		measureSpinner.setAdapter(new ArrayAdapter<ReportColumn>(getApplicationContext(),
				android.R.layout.simple_spinner_item, 
				GlobalState.reportService.getReportMeasureColumns(GlobalState.report)));
		
		measureSpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
		{

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int index, long arg3) {
				ReportColumn col = (ReportColumn) measureSpinner.getAdapter().getItem(index);
				synchronized (chartView.getSurfaceHolder())
				{
					if(GlobalState.chartModel.getMeasureColumn() != col)
					{
						GlobalState.chartModel.setMeasureColumn(col);
						GlobalState.chartModel.refreshReport();
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		sortSpinner = (Spinner) findViewById(R.id.sortSpinner);
		sortOrderArrayAdapter = new ArrayAdapter<Comparator<Sample>>(
				getApplicationContext(),
				android.R.layout.simple_spinner_item, 
				ComparatorManager.getInstance().getComparators());
		sortSpinner.setAdapter(sortOrderArrayAdapter);
		
		sortSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int index, long arg3)
			{
				Comparator<Sample> comparator = 
						(Comparator<Sample>) sortSpinner.getAdapter().getItem(index);
				
					if(comparator.getClass() != 
						chartView.getThread().getComparator().getClass())
					{
						chartView.setComparator(comparator);
						//chartView.onDataChanged();
						GlobalState.chartModel.refreshReport();
					}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
				// TODO Auto-generated method stub
				
			}
		});
		
		backToColumnSelectionButton = (Button) findViewById(R.id.backToColumnSelectionButton);
		backToColumnSelectionButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
                finish();
			}
		});
		
		drillUpButton = (Button) findViewById(R.id.drillUpButton);
		drillUpButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				synchronized (chartView.getSurfaceHolder())
				{
					GlobalState.chartModel.drillUp();	
				}
			}
		});
		
		GlobalState.chartModel.setHierarchy(GlobalState.selectedColumns);
		GlobalState.chartModel.setMeasureColumn((ReportColumn) measureSpinner.getAdapter().getItem(0));
		GlobalState.chartModel.setReport(GlobalState.report);
		GlobalState.chartModel.setListener(chartView);
		
		chartView.setModel(GlobalState.chartModel);
		chartView.setComparator((Comparator<Sample>) sortSpinner.getAdapter().getItem(0));
		GlobalState.chartModel.refreshReport();
	}

	@Override
	protected void onPause()
	{
		chartView.getThread().setRunning(false);
		super.onPause();
	}
	
	@Override
	protected void onResume()
	{
		if(chartView.getThread() != null)
			chartView.reinitializeThread();
		super.onResume();
	}
}
